module EntertainmentHelper
  
  $announcement_type = "entertainment"
  
  def query_entertainment_events show
      result = select_events_in(show, $announcement_type)
      entertainments = unless result== nil then result else Array.new end
      return entertainments
  end

  def write_date (time)
    current_time = time.to_s
    current_time = current_time.split(" ");
    return @month = current_time[2] + " " + current_time[1] + " @ " + current_time[3]
  end

  # this is a workaround mehtod since the query in the show method does not work!!!. Is not comparing the time stamps!
  def select_events_in(offset, type)
    current_time = Time.now
    add_time = (60 * 60 * 24)*offset
    in_a_time = current_time + add_time
 
    announcements = Announcement.find_by_sql("SELECT * FROM announcements WHERE announcement_type = '#{type}' order by start asc")
    
    counter = 0
    result = []
      
    announcements.each do |a|
      if(offset == 1) 
        if((a.end >= current_time) && (a.end.day == current_time.day))
          result[counter] = a
          counter+=1
        end
      elsif((a.end >= current_time) && (a.end <= in_a_time)) 
        result[counter] = a
        counter+=1
      end
      if(a.end < current_time) then a.destroy end
    end
    return result
  end
  
end
